import { createRouter, createWebHistory } from 'vue-router'
import Layout from '../layout/layout.vue'

const router = createRouter({
  history: createWebHistory(''),
  routes: [
    {
      path: '',
      name: 'login',
      component: () => import('../login/login.vue'),
      children: []
    },
    {
      path: '/layout',
      component: Layout,
      children: [
        {
          path: '/customer',
          component: () => import('../customer/customer.vue')
        },
        {
          path: '/setting',
          component: () => import('../setting/setting.vue')
        }
      ]
    }
  ]
})
router.beforeEach((to, from, next) => {
  const isAuthenticated = localStorage.getItem('accessToken');
  
  if (to.path !== '/' && !isAuthenticated) {
    if (confirm('请先登录\n\n点击确定前往登录页面')) {  // [!code ++]
      next('/');  // 确认后跳转登录页
    } else {
      next(false); // 取消则中断导航
    }
  } else {
    next();
  }
});

export default router